﻿Public Class ZeitrapportAdapter
    Inherits KundeProjektTaskFormAdapter(Of frmZeitrapport, ZeitrapportListener)
    Implements LoginServiceListener

    Private WithEvents btnSave As Button = form.btnSave
    Private WithEvents txtAufwand As TextBox = form.txtStunden

    Private loginMitarbeiter As Mitarbeiter

    Public Sub New()
        MyBase.New(New frmZeitrapport())
        setComboboxes(form.cbxKunde, form.cbxProjekt, form.cbxTask)
        Context.KUNDE_PROJEKT_TASK_SERVICE.getObservable().addListener(Me)
        Context.LOGIN_SERVICE.getObservable().addListener(Me)
    End Sub

    Private Sub btnSave_click() Handles btnSave.Click
        Dim zeitrapport As New Zeitrapport()
        zeitrapport.task = Me.getSelectedTask()
        zeitrapport.mitarbeiter = Me.loginMitarbeiter
        zeitrapport.kommentar = form.txtKommentar.Text
        Try
            zeitrapport.anzahlMinuten = parseHHmm(form.txtStunden.Text)
        Catch exception As Exception
            form.txtStunden.Focus()
            form.txtStunden.SelectAll()
            showError(exception.Message)
            Exit Sub
        End Try
        Try
            zeitrapport.validate()
        Catch ex As Exception
            showError(ex.Message)
            Exit Sub
        End Try
        form.txtStunden.Text = ""
        For Each listener As ZeitrapportListener In getObservable().getListeners()
            listener.save(zeitrapport)
        Next
    End Sub

    'formatiert das format mm nach HH:mm
    Private Sub formatAufwand() Handles txtAufwand.LostFocus
        Dim minuteString As String = txtAufwand.Text
        If Not IsNumeric(minuteString) Then
            ' abbrechen, entweder ist das format falsch oder bereits im Format hh:mm
            Exit Sub
        End If

        Dim minutes As Integer = Val(minuteString)
        Dim hours As Integer = minutes / 60
        minutes = minutes Mod 60

        txtAufwand.Text = Format(hours, "00") & ":" & Format(minutes, "00")
    End Sub

    ' parse the minutes of hh:mm
    Private Function parseHHmm(ByVal timeString As String) As Integer
        Dim tokens() As String = timeString.Split(":")

        If tokens.Length <> 2 Then
            Throw New Exception("Zeitformat ist ungültig")
        End If

        Dim hourString As String = tokens(0)
        Dim minuteString As String = tokens(1)

        If Not IsNumeric(hourString) Or Not IsNumeric(minuteString) Then
            Throw New Exception("Keine Gültige Zahl")
        End If

        Dim hours As Integer = Val(hourString)
        Dim minutes As Integer = Val(minuteString)
        Return hours * 60 + minutes
    End Function

    Public Sub loginMitarbeiterChanged(ByRef newMitarbeiter As Mitarbeiter) Implements LoginServiceListener.loginMitarbeiterChanged
        Me.loginMitarbeiter = newMitarbeiter
        form.txtMitarbeiter.Text = newMitarbeiter.getNiceName()
    End Sub

    Protected Overrides Sub onComboboxChange()
        btnSave.Enabled = Me.areRequiredComboboxesFilled()
    End Sub

End Class
